package com.demo.service.app.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.demo.service.app.pojo.FanPor;
import com.demo.service.app.pojo.FanPorArt;
import com.demo.service.app.pojo.Tag;
import com.demo.service.app.pojo.TagResult;
import com.demo.service.app.service.FanPorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author O
 */
@RestController
@RequestMapping("/fan")
public class FanPorController {
    private static final String BASE_URL = "";

    private final FanPorService fanPorService;

    @Autowired
    public FanPorController(FanPorService fanPorService) {
        this.fanPorService = fanPorService;
    }

    @GetMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("导出测试_User", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + "_" + System.currentTimeMillis() +".xlsx");
        String baseUrl = "";
        List<FanPor> fanPorList = fanPorService.getUser(baseUrl);
        EasyExcel.write(response.getOutputStream(), FanPor.class).sheet("performer").doWrite(fanPorList);
    }

    @GetMapping("/download-perf")
    public void downloadPerf(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("文档导出测试_PERF", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + "_" + System.currentTimeMillis() +".xlsx");

        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
        //这里 需要指定写用哪个class去写
        List<FanPor> fanPorList = fanPorService.getUser(BASE_URL);
        WriteSheet writeSheet = EasyExcel.writerSheet(0, "performer").head(FanPor.class).build();
        excelWriter.write(fanPorList, writeSheet);

        //遍历performer
        List<FanPorArt> fanPorArtList = fanPorService.getArts(fanPorList);
        writeSheet = EasyExcel.writerSheet(1, "article").head(FanPorArt.class).build();
        excelWriter.write(fanPorArtList, writeSheet);
        //千万别忘记finish 会帮忙关闭流
        excelWriter.finish();
    }

    @GetMapping("/download-tag")
    public void downloadByTag(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("文档导出测试_TAG", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + "_" + System.currentTimeMillis() +".xlsx");

        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
        //这里 需要指定写用哪个class去写

        List<Tag> list = fanPorService.getTags(BASE_URL);
        List<Tag> distinctList = list.stream().distinct().collect(Collectors.toList());
        WriteSheet writeSheet = EasyExcel.writerSheet(0, "Tags").head(Tag.class).build();
        excelWriter.write(distinctList, writeSheet);

        //遍历标签
        for(int i=0; i<distinctList.size(); i++){
            Tag tag = distinctList.get(i);
            List<TagResult> tagResultList = fanPorService.getTagResult(tag.getName(), tag.getLink());
            writeSheet = EasyExcel.writerSheet(i+1, tag.getName()).head(TagResult.class).build();
            excelWriter.write(tagResultList, writeSheet);
        }
        //千万别忘记finish 会帮忙关闭流
        excelWriter.finish();
    }

}
